package cn.melon.main_frame;

import java.io.IOException;

import javax.swing.SwingUtilities;

import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.bz.ClientThread;
import cn.melon.bz.ClientThreadMgr;
import cn.melon.common.ModelType;
import cn.melon.helper.ICacheHelper;
import cn.melon.helper.IOnRequestListener;
import cn.melon.helper.MemCacheHelper;
import cn.melon.helper.RequestHelper;
import cn.melon.mod.login.UserInfo;
import cn.melon.window.AdminMainWindow;
import cn.melon.window.MainWindow;

public class AdminMain {


    private void onLogin() {
        JSONObject reqJSON = new JSONObject(); 
        String reqStr = null; 
        String username = "a";
        String password = "a";
        String cardNum = username; 
        String pass = password; 
        
        try {
            reqJSON.put("student_num", cardNum);
            reqJSON.put("password", pass);
            reqStr = reqJSON.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.LOGIN)
            .addRequest(reqStr)
            .addReceiver("login_recv", new IOnRequestListener() {
                
                @Override
                public void onSuccess(String response) {

                    Boolean logSuccess = false;
                    String logType = null;
                    
                    try {
                        JSONObject jsonRes = new JSONObject(response);
                        
                        UserInfo info = new UserInfo();
                        if (jsonRes.getInt("code") == 200) {
                            JSONObject jsonContent = jsonRes.getJSONObject("content");
                            
                            // 下面线程操作只有登录会用到, 其它模块直接使用response即可
                            ClientThread thread =  ClientThreadMgr.get("tmpThread");

                            // 将uuid, 保存在缓存中
                            ICacheHelper helper = new MemCacheHelper();
                            helper.setCache("uuid", jsonContent.getString("uuid"));
                            helper.setCache("username", username);
                            logType = jsonContent.getString("type");
                            ClientThreadMgr.add(jsonContent.getString("uuid"), thread);

                            logSuccess = true;

                            info.setStudentNum(username);
                            info.setPassword(password);
                            info.setType(logType);
                            info.setUUID(jsonContent.getString("uuid"));
                        }
                        
                        if(logSuccess) { 
                        }
                        
                        if (logType == null) {
                        } else if(logType.equals("student")){   // 登录到学生的界面 
                            SwingUtilities.invokeLater(new Runnable() {
                                
                                @Override
                                public void run() {
                                    MainWindow m = new MainWindow(info);//打开主界面
                                    m.setVisible(true); 
                                }
                            });
                        } else if (logType.equals("admin")){ // 登录到管理员界面 
                            SwingUtilities.invokeLater(new Runnable() {
                                @Override
                                public void run() {
                                    AdminMainWindow adminMainWindow = new AdminMainWindow();
                                    adminMainWindow.setVisible(true);
                                }
                            });
                        } 

                        //ClientThreadMgr.remove("tmpThread");
                        
                    } catch (JSONException | IOException e) {
                        e.printStackTrace();
                    }
                    
                }
            }).build().start();
    }
    
    public static void main(String[] args) {
        new AdminMain().onLogin();
    }
}
